<template>
  <basic-container :radius="0">
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :before-open="beforeOpen"
      ref="crud"
      v-model="form"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="primary" icon="el-icon-plus" size="small" @click.stop="handleSave">新 建</el-button>
      </template>
      <template slot-scope="{row}" slot="brandLogo">
        <div class="haruyuki-flex-center">
          <el-image  style="width: 48px; height: 48px" :src="row.brandLogo" :preview-src-list="[row.brandLogo]"> </el-image>
        </div>
      </template>
    </avue-crud>
    <!-- 新建 -->
    <el-dialog
      title="设备列表"
      append-to-body
      width="440px"
      :close-on-click-modal="false"
      v-if="firstDialog.visible"
      :visible.sync="firstDialog.visible"
      :before-close="beforeClose"
    >
      <el-form :model="firstForm" :rules="firstRules" ref="firstForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="设备类型" prop="brandId">
          <el-select size="small" v-model="firstForm.brandId" @change="brandIdChange" filterable placeholder="请选择设备类型">
            <el-option :label="item.brand_name" :value="item.brand_id" v-for="item in firstData.brandList" :key="item.brand_id">
                <span style="float: left">{{ item.brand_name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.brand_id }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="插件类型" prop="uuid">
          <el-select size="small" v-model="firstForm.uuid" @change="queryV5Id" filterable placeholder="请选择插件类型">
            <el-option :label="item.name" :value="item.uuid" v-for="item in firstData.gwwList" :key="item.uuid">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.uuid }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="插件子类" prop="softUid">
          <el-select size="small" v-model="firstForm.softUid" @change="thirdChange" filterable placeholder="请选择插件类型">
            <el-option :label="item.name" :value="item.uuid" v-for="item in thirdData" :key="item.uuid">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.uuid }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" :disabled="disabled" @click="submitForm('firstForm')">保存</el-button>
          <el-button size="small" @click="resetForm('firstForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </basic-container>  
</template>

<script>
import { mapGetters } from "vuex";
// import { getList,apiLogonCmf,apiEqTypeListCmf,apiQueryV5Id,apiBindRpiTerminalCmf } from "@/api/rb-core/posinfo/pluginUnit/pluginUnit";
import selectPage from "@/components/select-page/select-page";
// import md5 from 'js-md5';
export default {
  components: {
    selectPage
  },
  props: {
    rptNo: {
      type: String,
      required: true,
      default:""
    }
  },
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        // height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        searchLabelWidth: 130, //搜索框标题宽度
        columnBtn: false,
        labelWidth:100,
        addBtn: false,//取消表格 新建/添加
        editBtn:false,
        viewBtn:false,
        delBtn:false,
        viewTitle:'修 改',
        menuWidth: 100,
        menu:false,

        column: [
          {
            label: "商户编号",
            prop: "rpiNo",
            type: "input",
            minWidth:120,
            overHidden:true
          },
          {
            label: "商户名称",
            prop: "rpiName",
            type: "input",
            display: false,
            minWidth:120,
            overHidden:true
          },
          {
            label: "设备类型id",
            prop: "brandId",
            type: "input",
            search:true,
            overHidden:true
          },
          {
            label: "设备类型说明",
            prop: "brandDesc",
            type: "input",
            overHidden:true
          },
          {
            label: "设备类型logo",
            prop: "brandLogo",
            type: "input",
            overHidden:true
          },
          {
            label: "设备类型名称",
            prop: "brandName",
            type: "input",
            search:true,
            overHidden:true
          },
          {
            label: "子类名称",
            prop: "name",
            type: "input",
            search:true,
            overHidden:true
          },
          {
            label: "终端号",
            prop: "shortKey",
            type: "input",
            search:true,
            overHidden:true
          },
          {
            label: "设备sn",
            prop: "sn",
            type: "input",
            search:true,
            overHidden:true
          },
          {
            label: "子类uid",
            prop: "uuid",
            type: "input",
            search:true,
            overHidden: true
          },
          {
            label: "萤火虫秘钥",
            prop: "yhcSecretKey",
            type: "input"
          }
          
        ]
      },
      data: [],
      /* 商户编号 */ 
      rpiNo: "",
      /* 聪明付商户绑定 */ 
      logonCmfData:{},
      /* 第一个弹框 */ 
      firstDialog: {
        disabled: false,
        visible: false
      },
      /* 第一个列表 */ 
      firstData:{},
      firstForm:{
        brandId:"",
        uuid:"",
        softUid:""
      },
      firstRules:{
        brandId: [
          { required: true, message: '请选择设备类型', trigger: ['blur','change'] },
        ],
        uuid: [
          { required: true, message: '请选择插件类型', trigger: ['blur','change'] }
        ],
        softUid: [
          { required: true, message: '请选择插件子类', trigger: ['blur','change'] }
        ],
      },
      selectFirstData:{},
      thirdData:[],
      disabled:false
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    // permissionList() {
    //   return {
    //     addBtn: this.vaildData(this.permission.terminal_add, false),
    //     viewBtn: this.vaildData(this.permission.terminal_view, false),
    //     delBtn: this.vaildData(this.permission.terminal_delete, false),
    //     editBtn: this.vaildData(this.permission.terminal_edit, false),
    //   };
    // },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  watch: {},
  created() {
    this.rpiNo = this.$route.query.rpiNo;
    this.logonCmf(); /* 聪明付商户绑定 */ 
  },
  methods: {
    /**
      * @func 保存
      * */
    submitForm(formName) {
      let _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let param = Object.assign(_this.firstForm,this.selectFirstData,{rpiNo:_this.rpiNo});
          delete param.uuid;
          _this.disabled = true;

          apiBindRpiTerminalCmf(param).then( res =>{
            _this.resetForm('firstForm');
            _this.firstDialog.visible = false;
            _this.disabled = false;
            
            _this.$message({
              type: "success",
              message: res.data.msg
            });

            // _this.$root.Bus.$emit('pluginSub'); //传播方法
            _this.onLoad(this.page);
          },error =>{
            _this.disabled = false;
            window.console.log(error);
          })
        } else {
          return false;
        }
      });
    },
    /**
    * @func 选中-插件子类
    * */
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    /**
     * @func 选中-插件子类
     * */
    thirdChange(val){
      const index =  this.thirdData.findIndex(obj => obj.uuid === val );
      this.firstForm.name = this.thirdData[index].name;
      this.$forceUpdate();
    },
    /**
     * @func 弹框
     * */
    beforeClose(done){
      this.$refs["firstForm"].resetFields();
      done();
    },
    /**
     * @func 选择——设备类型
     * */
    brandIdChange(val){
      const index =  this.firstData.brandList.findIndex(obj => obj.brand_id === val );
      this.selectFirstData = {
        brandDesc: this.firstData.brandList[index].brand_desc,
        brandId: this.firstData.brandList[index].brand_id,
        brandLogo: this.firstData.brandList[index].logo,
        brandName: this.firstData.brandList[index].brand_name
      }
      this.$forceUpdate();
    },
    /**
     * @func 查询插件列表
     * */
    queryV5Id(val){
      this.firstForm.softUid = "";
      this.$forceUpdate();

      apiQueryV5Id({uuid:val}).then(res =>{
        this.thirdData = res.data.data;
      })
    },
    /**
     * @func 新建
     * */
     handleSave() {
      this.firstDialog.disabled = false;
      this.firstDialog.visible = true;

      this.eqTypeListCmf();
    },
    /**
     * @func 设备列表查询
     * */
    eqTypeListCmf(){
      apiEqTypeListCmf(this.logonCmfData).then(res =>{
        this.firstData = res.data.data;
      })
    },

    /**
     * @func 聪明付商户绑定
     * */
    logonCmf(){
      apiLogonCmf({rpiNo:this.rpiNo}).then(res=>{
        this.logonCmfData = res.data.data;
      })
    },
    

    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, { rpiNo: this.rpiNo })//,rpmNo:'R00001'
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.$emit('total');
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    beforeOpen(done, type) {
      if (["view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    }
  }
};
</script>

<style>
</style>
